/*
 * Copyright (c) 2011, 2012 Alexander Kluth <alexander.kluth@outlook.com>
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package hardyhttp.server.helper;

import hardyhttp.filesystem.Filesystem;
import hardyhttp.ressources.Constants;
import java.io.File;

/**
 * Class providing some HTML-templates
 * 
 * @author akluth
 */
public class HTMLTemplates 
{
    Filesystem fs = new Filesystem();
    
    
    public HTMLTemplates()
    {
        
    }
    
    
    /**
     * HTML-Heading
     * 
     * @param title of the webpace
     * @return html string
     */
    public String insertHTMLHeading(String title)
    {
        return "<html><head><title>" + title + "</title></head><body>";
    }
    
    
    /**
     * HTML-Footer
     * 
     * @return html string
     */
    public String insertHTMLFooter()
    {
        return "</body></html>";
    }
    
    
    /**
     * Create the required HTML for formatted display of a directorys content.
     * We use StringBuilder here, this causes more method-calls, but in the end
     * StringBuilder is way faster than simply appending Strings together.
     * 
     * @param directory
     * @param files
     * @return The created HTML
     */
    public String insertDirectoryContent(String directory, String htdocs, String[] filesabs, String[] filesrel)
    {
        StringBuilder tmp = new StringBuilder("");
        String abspath = htdocs + directory + "/";
        
        tmp.append("<div style='font-family: Verdana; font-size: 0.8em;'>Contents of ");
        tmp.append(directory);
        tmp.append("<br /><hr />");
        tmp.append("<table border='0' cellspacing='10' style='font-family: Verdana; font-size: 0.9em;'><tr><td><b>Name</b></td><td>Type</td><td>Size</td><td>Attributes</tr>");
        
        tmp.append("<tr><td><a href='");
        tmp.append(directory);
        //FIXME: Many / are appended...but not under Linux o.O
        tmp.append("/..'>..</a></td><td></td><td></td><td></td></tr>");
        
        // Is there a "double" foreach-loop in Java?
        for (int i = 0; i < filesabs.length; i++)
        {
            tmp.append("<tr><td><a href='/");
            tmp.append(filesabs[i]);
            tmp.append("'>");
            tmp.append(filesrel[i]);
            tmp.append("</a></td>");
            tmp.append("<td>");
            
            if (fs.isDir(fs.getCwd() + File.separator + directory + filesabs[i]) == true)
                tmp.append("dir");
            
            else
                tmp.append("file");
            
            tmp.append("</td><td>");
            tmp.append(Integer.toString(fs.fileSize(abspath + filesrel[i])));
            tmp.append("</td><td>");
            
            if (fs.canRead(abspath + filesrel[i]) == true)
                tmp.append("r");
            
            if (fs.canWrite(abspath + filesrel[i]) == true)
                tmp.append("w");
            
            if (fs.canExec(abspath + filesrel[i]) == true)
                tmp.append("x");
            
            tmp.append("</td></tr>");
        }
        
        tmp.append("</table></div><hr />");
        tmp.append("<div style='font-family: Verdana; font-size: 0.8em;'>");
        tmp.append("Server running ");
        tmp.append(Constants.program_name);
        tmp.append("/");
        tmp.append(Constants.version);
        tmp.append(" on ");
        tmp.append(Constants.vm_name);
        tmp.append(" version ");
        tmp.append(Constants.vm_version);
        tmp.append(" (");
        tmp.append(Constants.os);
        tmp.append(")");
        
        return tmp.toString();
    }
}
